package com.imphead.demo1.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    // 处理所有异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleAllExceptions(Exception ex) {
        // 记录通用异常日志
        //SystemMain.log.error("An unexpected error occurred: ", ex);
        log.error("An unexpected error occurred: ", ex);
        return handleException(new ApiErrorResponse("GEN001", "An unexpected error occurred"));
    }

    // 处理自定义异常
    @ExceptionHandler(CustomException.class)
    public ResponseEntity<Object> handleCustomException(CustomException ex) {
        // 记录自定义异常日志
        //SystemMain.log.error("Custom exception occurred: ", ex);
        log.error("Custom exception occurred: ", ex);
        return handleException(new ApiErrorResponse(ex.getErrorCode(), ex.getErrorMessage()));
    }

    // 处理其他一般异常
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<Object> handleGeneralException(RuntimeException ex) {
        // 记录一般异常日志
        //SystemMain.log.error("General runtime exception occurred: ", ex);
        log.error("General runtime exception occurred: ", ex);
        return handleException(new ApiErrorResponse("GEN002", "A general runtime exception occurred"));
    }

    // 辅助方法，用于创建并返回错误响应
    private ResponseEntity<Object> handleException(ApiErrorResponse errorResponse) {
        //SystemMain.log.error("General runtime handleException occurred: ", errorResponse);
        log.error("General runtime handleException occurred: ", errorResponse);
        return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    // ApiErrorResponse 类
    public static class ApiErrorResponse {
        private String errorCode;
        private String errorMessage;

        public ApiErrorResponse(String errorCode, String errorMessage) {
            this.errorCode = errorCode;
            this.errorMessage = errorMessage;
        }

        // Getter 和 Setter 方法
        public String getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }

        public String getErrorMessage() {
            return errorMessage;
        }

        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }

        // toString 方法
        @Override
        public String toString() {
            return "ApiErrorResponse{" +
                    "errorCode='" + errorCode + '\'' +
                    ", errorMessage='" + errorMessage + '\'' +
                    '}';
        }
    }
}
